package sn.gandal.agriculture.agrimobile.j2me.gui;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.Membre;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.BusinessServicesImpl;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.IBusinessServices;
import sn.gandal.agriculture.agrimobile.j2me.utilities.Configuration;

/**
 *
 * @author dysow
 */
public class RechercheAbonneForm extends Form implements CommandListener
{

    private Display display;
    private Displayable previousScreen;
    private TextField txtAbonneId;
    private TextField txtNom;
    private TextField txtPrenom;
    private TextField txtNomMere;
    private TextField txtPrenomMere;
    private Command cmdOK;
    private Command cmdBack;
    private String previousMenuTitle;
    private String previousItemSelected;
    public static final int MAXIMUM_SIZE = 100;//définit le nombre de caractères à mettre
    public static final int MAXIMUM_SIZE_ID = 8;//définit le nombre de chiffres à mettre pour l'identifiant
    private RechercheAbonneList rechercheAbonneList = null;

    private RechercheAbonneForm(Display display)
    {

	super(Configuration.RECHERCHE_ABONNE_FORM);

	this.display = display;
	this.initialiseComponents();
	this.initialiseCommands();
	this.addComponents();
	this.addCommands();
	this.setCommandListener(this);
    }

    public RechercheAbonneForm(Display display, String previousItem, String previousMenuTitle)
    {
	this(display);
	System.out.println("Les données à initialiser : " + previousItem);
	// System.out.println("Les données à initialiser : "+previous);
	previousItemSelected = previousItem;
	this.previousMenuTitle = previousMenuTitle;
    }

    private void initialiseComponents()
    {
	this.txtAbonneId = new TextField("Identifiant Abonné", "", RechercheAbonneForm.MAXIMUM_SIZE_ID, TextField.NUMERIC);
	this.txtNom = new TextField("Nom Abonné", "", RechercheAbonneForm.MAXIMUM_SIZE, TextField.ANY);
	this.txtPrenom = new TextField("Prénom Abonné", "", RechercheAbonneForm.MAXIMUM_SIZE, TextField.ANY);
}

    private void initialiseCommands()
    {
	this.cmdOK = new Command("Rechercher", Command.SCREEN, 1);
	this.cmdBack = new Command("Back", Command.BACK, 2);
    }

    private void addComponents()
    {
	this.append(this.txtAbonneId);
	this.append(this.txtNom);
	this.append(this.txtPrenom);
	this.append(this.txtNomMere);
	this.append(this.txtPrenomMere);
    }

    private void addCommands()
    {
//         if(previousScreen.getTitle().equals(Configuration.MENU_PRINCIPAL)){
//
//        }
	this.addCommand(this.cmdOK);
	this.addCommand(this.cmdBack);
    }

    /**
     * Recherche des abonnés
     */
    public void rechercherAction()
    {
	int abonneId = -1;
	String strAbonneId = this.txtAbonneId.getString();

	if (strAbonneId.length() > 0)
	{
	    abonneId = Integer.parseInt(strAbonneId);
	}
	String prenom = this.txtPrenom.getString();
	String nom = this.txtNom.getString();
	String prenomMere = this.txtPrenomMere.getString();
	String nomMere = this.txtNomMere.getString();

	Membre abonne = new Membre(abonneId, prenom, nom);

	IBusinessServices businessServices = BusinessServicesImpl.getInstance();

	ControleFormsUtil controleFormsUtil = new ControleFormsUtil();
	String validateRecherche = controleFormsUtil.controleRechercheAbonneForm(abonne);


	if (validateRecherche.length() > 0)
	{
//	    Alert alert = new Alert("Erreur recherche", "Une erreur est survenue : " + validateRecherche, null, AlertType.ERROR);
//	    this.display.setCurrent(alert);
	    this.display.setCurrent(new ErrorsForm(validateRecherche), this);
	}
	else
	{

	    Membre[] abonneList = businessServices.getRechercheAbonneList(abonneId, prenom, nom, prenomMere, nomMere);

	    if (abonneList.length > 0)
	    {
		this.display.setCurrent(this.getRechercheAbonneList());
		this.getRechercheAbonneList().populateAbonneList(abonneList);
		this.display.setCurrent(this);

	    }
	    else
	    {
//		Alert alert = new Alert("Résultat de recherche", "Il n'y a aucune résultat pour cette recherche", null, AlertType.ERROR);
//		this.display.setCurrent(alert);
		String error = "Il n'y a aucune résultat pour cette recherche";
		this.display.setCurrent(new ErrorsForm(error), this);
	    }

	}
    }

    public void backAction()
    {

//        if(previousItemSelected==null){
//            this.display.setCurrent(new MainMenu(MainMenu.getMidlet()));
//        }else{
	if (previousMenuTitle.equals(Configuration.MENU_PRINCIPAL))
	{
	    this.display.setCurrent(new MainMenu(MainMenu.getMidlet()));
	}
	else if (previousMenuTitle.equals(Configuration.MENU_GESTION_MEMBRES))
	{
	    this.display.setCurrent(new GestionMembreMenu(display));
	}
	else if (previousMenuTitle.equals(Configuration.MENU_GESTION_PRODUCTION))
	{
	    this.display.setCurrent(new GestionConvocationsMenu(display));
	}

    //}
    }

    public void commandAction(Command c, Displayable d)
    {
	if (c.equals(this.cmdOK))
	{
	    this.rechercherAction();
	}
	else if (c.equals(this.cmdBack))
	{
	    this.backAction();
	}
    }

    /**
     * Avoir la liste des abonnés recherchés
     * @return
     */
    private RechercheAbonneList getRechercheAbonneList()
    {
	if (this.rechercheAbonneList == null)
	{
	    System.out.println("Item selecetionné et qui sera passé en param :" + previousItemSelected);
	    this.rechercheAbonneList = new RechercheAbonneList(this.display, this,previousMenuTitle, previousItemSelected);

	}
	return this.rechercheAbonneList;
    }
    public void setPreviousScreen(Displayable displayable)
    {
	this.previousScreen = displayable;
    }
//    public void setPreviousItemTitle(String previousItemTitle) {
//        this.previousItemSelected = previousItemTitle;
//    }
//
//    private void getPreviousScreen() {
//        if(previousItemSelected.equals(Configuration.MENU_GESTION_MEMBRES)){
//           //Displayable previousScreen= MainMenu.getMidlet();
//        }else{
//
//        }
//    }
}
